<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="发送内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入发送内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送范围" prop="unitName">
        <el-select
            v-model="queryParams.unitName"
            placeholder="请选择"
            clearable
            size="small"
            style="width: 300px"
          >
            <el-option
              v-for="(dict, index) in unitNameList"
              :key="index"
              :label="dict.unitName"
              :value="dict.unitName"
            />
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['committee:information:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['committee:information:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['committee:information:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['committee:information:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="msgid" /> -->
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column label="发送时间" align="center" prop="createTime" width="180" />
      <el-table-column label="发送人" align="center" prop="nickName" />
      <el-table-column label="发送内容" align="center" prop="content" />
      <el-table-column label="发送范围" align="center" prop="unitName" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['committee:information:edit']"
            >修改</el-button
          > -->
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['committee:information:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改消息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="发送范围" prop="unitCode">
          <el-select
            v-model="form.unitCode"
            placeholder="请选择"
            clearable
            size="small"
            style="width: 300px"
          >
            <el-option
              v-for="(dict, index) in unitNameList"
              :key="index"
              :label="dict.unitName"
              :value="dict.unitCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发送内容">
          <el-input
            v-model="form.content"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 8 }"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 确认详情列表 -->
    <el-dialog :title="title" :visible.sync="detailListOpen" width="800px" append-to-body>
        <span slot="title" class="dialog-footer">
            {{title}}
            <el-button style="margin:0 0 0 550px;" size="mini" type="primary" @click="handleRemind">一键提醒</el-button>
          </span>
      <el-table
        v-loading="loading"
        :data="detailList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column label="姓名" align="center" prop="nickname" width="180"/>
        <el-table-column label="学号" align="center" prop="username"/>
        <el-table-column label="时间" align="center" prop="foundtime"/>
        <el-table-column label="状态" align="center" prop="isconfirm">
          <template slot-scope="scope">
            <span>{{ scope.row.state == 1 ? '已确认' : scope.row.state == 0 ?"未确认": '无需确认' }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInformation,
  getInformation,
  delInformation,
  addInformation,
  updateInformation,
  getUserTeam,
  FindOrgInfoByStudentId,
  getConfirmdetail,
  getCollege
} from '@/api/information';

export default {
  name: 'Information',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息表格数据
      informationList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      detailListOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      sendcontentList: [
        '提醒学校负责人落实打卡',
        '提醒系部负责人落实打卡',
        '提醒团队负责人落实打卡',
        '提醒团队人员打卡',
      ],
      // 表单参数
      form: {
        content:null,
        unitCode:null
      },
      // 表单校验
      rules: {
        msgtype: [{ required: true, message: '消息类型不能为空', trigger: 'blur' }],
        sendscope: [{ required: true, message: '发送范围不能为空', trigger: 'blur' }],
        sendscope2: [{ required: true, message: '具体发送范围不能为空', trigger: 'blur' }],
        // outerInterlinkage: [{ required: true, message: '链接不能为空', trigger: 'blur' }],
        sendcontent: [{ required: true, message: '发送内容模板不能为空', trigger: 'blur' }],
        isconfirm: [{ required: true, message: '是否需要确认不能为空', trigger: 'blur' }],
      },
      unitNameList: [], //学院列表
      teamOptions: [], //团队列表
      activityOptions: [], //团队列表
      orgInfoArr: [], //社团列表

      detailList:[],//确认详情列表

      curMsgid:null,//当前信息id
    };
  },
  created() {
    this.getList();
    getCollege().then(response => {
      this.unitNameList = response.rows;
    });

    // this.getTeamList();
    // this.orgInfoArrList();
    // this.getActivityList();
  },
  methods: {
    /** 查询消息列表 */
    getList() {
      this.loading = true;
      listInformation(this.queryParams).then(response => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // getTeamList() {
    //   getUserTeam().then(response => {
    //     this.teamOptions = response.data;
    //   });
    // },
    // orgInfoArrList(val) {
    //   var query = { orgName: val };
    //   this.searchLoading = true;
    //   FindOrgInfoByStudentId(query).then(response => {
    //     this.searchLoading = false;
    //     this.orgInfoArr = response.data;
    //   });
    // },
    // getActivityList(val) {
    //   var query = { pageNum: 1, pageSize: 30, orgName: val };
    //   this.searchLoading = true;
    //   pullDownList(query).then(response => {
    //     this.searchLoading = false;
    //     this.activityOptions = response.rows;
    //   });
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        content:null,
        unitCode:null
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.msgId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加消息';
    },
    // 确认详情按钮
    confirmdetail(row) {
        const msgid = row.msgid || this.ids;
        this.curMsgid = msgid;
      getConfirmdetail(msgid).then(response => {
          this.detailList = response.data;
          this.detailListOpen = true;
        this.title = '确认详情';
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const msgid = row.msgid || this.ids;
      getInformation(msgid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改消息';
      });
    },
    changeSendscope(){
        this.form.sendscope2 = null;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.msgid != null) {
            updateInformation(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addInformation(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const msgId = row.msgId || this.ids;
      this.$modal
        .confirm('是否确认删除消息编号为"' + msgId + '"的数据项？')
        .then(function () {
          return delInformation(msgId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 一键提醒按钮操作 */
    handleRemind(row) {
    //   const msgids = row.msgid || this.ids;
      this.$modal
        .confirm('是否一键提醒？')
        .then(function () {
        //   return delInformation(this.curMsgid);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('操作成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'committee/information/export',
        {
          ...this.queryParams,
        },
        `information_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
